<!-- build time:Tue Jul 21 2020 12:41:23 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/blog_favicon/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/blog_favicon/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><meta name="baidu-site-verification" content="r15J5zOS2KI43yBS"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css"><script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:new URL("http://www.zhengjiaao.cn").hostname,root:"/",scheme:"Muse",version:"7.6.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:null},back2top:{enable:!0,sidebar:!1,scrollpercent:!0},bookmark:{enable:!0,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!1,pangu:!1,comments:{style:"tabs",active:"valine",storage:!0,lazyload:!1,nav:null,activeClass:"valine"},algolia:{appID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},path:"search.xml",motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}}}</script><meta property="og:type" content="article"><meta property="og:title" content="史上最全Websocket通信测试"><meta property="og:url" content="http://www.zhengjiaao.cn/posts/34408947.html"><meta property="og:site_name" content="宇宙小神特别萌"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-6fe5454990797077.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/500"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-d3b4fc6eca42e317.jpg"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-8f0932bdb9bb0a0a.jpg"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-a33945bcc1221aaa.jpg"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-f561128226820a01.jpg"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-a6956015544c5a75.jpg"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-1f7a161fe39b7311.jpg"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-936f534ce7a3f8db.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-54c7d906ce153b97.png"><meta property="article:published_time" content="2020-01-03T08:14:50.000Z"><meta property="article:modified_time" content="2020-07-20T10:37:55.308Z"><meta property="article:author" content="宇宙小神特别萌"><meta property="article:tag" content="WebScoket"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/15645795-6fe5454990797077.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/500"><link rel="canonical" href="http://www.zhengjiaao.cn/posts/34408947.html"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0}</script><title>史上最全Websocket通信测试 | 宇宙小神特别萌</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript><link rel="alternate" href="/atom.xml" title="宇宙小神特别萌" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-meta custom-logo"><div class="site-meta-headline"><a><img class="custom-logo-image" src="/img/blog_favicon/favicon-32x32-next.png" alt="宇宙小神特别萌"></a></div><div><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">宇宙小神特别萌</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle">ZhengJa</p></div><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">27</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">116</span></a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a></li><li class="menu-item menu-item-messageboard"><a href="/messageboard/" rel="section"><i class="fa fa-fw fa-address-book"></i>留言板</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="fa fa-fw fa-user-plus"></i>友链</a></li><li class="menu-item menu-item-shuoshuo"><a href="/shuoshuo/" rel="section"><i class="fa fa-fw fa-file-text-o"></i>说说</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="site-search"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocorrect="off" autocapitalize="none" placeholder="搜索..." spellcheck="false" type="text" id="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"></div></div><div class="search-pop-overlay"></div></div></div></header><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a> <a href="https://github.com/zhengjiaao" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content"><div class="posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="http://www.zhengjiaao.cn/posts/34408947.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/my-images/avatar.gif"><meta itemprop="name" content="宇宙小神特别萌"><meta itemprop="description" content="人生就是一次幸运的旅行！"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="宇宙小神特别萌"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">史上最全Websocket通信测试</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2020-01-03 16:14:50" itemprop="dateCreated datePublished" datetime="2020-01-03T16:14:50+08:00">2020-01-03</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2020-07-20 18:37:55" itemprop="dateModified" datetime="2020-07-20T18:37:55+08:00">2020-07-20</time> </span><span id="/posts/34408947.html" class="post-meta-item leancloud_visitors" data-flag-title="史上最全Websocket通信测试" title="热度"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span class="leancloud-visitors-count"></span> <span>℃</span> </span><span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span id="busuanzi_value_page_pv"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><span class="post-meta-item-text">Valine：</span> <a title="valine" href="/posts/34408947.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/34408947.html" itemprop="commentCount"></span></a></span><br><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>33k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>30 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><meta name="referrer" content="no-referrer"><a id="more"></a><p>H5和H5之间通信<br>H5和Andorid之间通信<br>Andorid和Andorid之间通信<br>H5通过http协议调用接口发送消息-给移动Andorid、H5等<br>后台支持语言：java<br>前端支持语言：Andorid（java）、H5（html）、jsp等<br>发送：发送给所有连接者、发送给所有在线者、发送给指定用户、及(发送对象)用户不在线处理<br>注：把连接地址改了 ，可以直接使用复制代码测试使用；</p><h3 id="H5和Andorid测试页面："><a href="#H5和Andorid测试页面：" class="headerlink" title="H5和Andorid测试页面："></a>H5和Andorid测试页面：</h3><table><thead><tr><th>Andorid测试页面 –消息发送者 发送指定用户 【消息接收者1】 ，也可以发送所有人</th><th></th></tr></thead><tbody><tr><td><img src="https://upload-images.jianshu.io/upload_images/15645795-6fe5454990797077.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/500" alt="Andorid测试页面.jpg"></td><td></td></tr></tbody></table><table><thead><tr><th>消息接收者1</th><th>消息接收者2</th></tr></thead><tbody><tr><td><img src="https://upload-images.jianshu.io/upload_images/15645795-d3b4fc6eca42e317.jpg" alt="测试页面1.jpg"></td><td><img src="https://upload-images.jianshu.io/upload_images/15645795-8f0932bdb9bb0a0a.jpg" alt="测试页面2.jpg"></td></tr></tbody></table><h5 id="通过Http发送测试页面"><a href="#通过Http发送测试页面" class="headerlink" title="通过Http发送测试页面"></a><strong>通过Http发送测试页面</strong></h5><table><thead><tr><th>发送消息给所有在线用户</th><th>发送消息给指定在线用户 –【测试用户 -张三】</th></tr></thead><tbody><tr><td><img src="https://upload-images.jianshu.io/upload_images/15645795-a33945bcc1221aaa.jpg" alt="http测试发送消息给所有在线用户.jpg"></td><td><img src="https://upload-images.jianshu.io/upload_images/15645795-f561128226820a01.jpg" alt="http测试发送消息给指定在线用户.jpg"></td></tr><tr><td>测试用户李四</td><td>测试用户张三</td></tr><tr><td><img src="https://upload-images.jianshu.io/upload_images/15645795-a6956015544c5a75.jpg" alt="测试用户【李四】.jpg"></td><td><img src="https://upload-images.jianshu.io/upload_images/15645795-1f7a161fe39b7311.jpg" alt="测试用户【张三】.jpg"></td></tr></tbody></table><h3 id="java后台代码"><a href="#java后台代码" class="headerlink" title="java后台代码"></a><strong>java后台代码</strong></h3><h5 id="引入依赖："><a href="#引入依赖：" class="headerlink" title="引入依赖："></a>引入依赖：</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--集成移动和H5的java_websocket--&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.java-websocket<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>Java-WebSocket<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>springboot项目结构 ，也可用spring项目 ，和框架没有关系 ，没有影响</p><p><img src="https://upload-images.jianshu.io/upload_images/15645795-936f534ce7a3f8db.png" alt="代码类路径.png"></p><h4 id="1-WebSocketPool自定义工具类"><a href="#1-WebSocketPool自定义工具类" class="headerlink" title="1.WebSocketPool自定义工具类"></a>1.WebSocketPool自定义工具类</h4><p>主要是连接池 ，存用户和WebSocket连接</p><p>自定义 WebSocketPool.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.utils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.java_websocket.WebSocket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebSocketPool</span> </span>&#123;</span><br><span class="line">    <span class="comment">//连接-用户名</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Map&lt;WebSocket, String&gt; userconnections = <span class="keyword">new</span> HashMap&lt;WebSocket, String&gt;();</span><br><span class="line">	  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 获取用户名 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> conn </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getUserByKey</span><span class="params">(WebSocket conn)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> userconnections.get(conn);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 获取在线总数 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getUserCount</span><span class="params">()</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">return</span> userconnections.size();  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 获取WebSocket </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> WebSocket <span class="title">getWebSocketByUser</span><span class="params">(String user)</span> </span>&#123;  </span><br><span class="line">        Set&lt;WebSocket&gt; keySet = userconnections.keySet();</span><br><span class="line">        <span class="keyword">synchronized</span> (keySet) &#123;  </span><br><span class="line">            <span class="keyword">for</span> (WebSocket conn : keySet) &#123;  </span><br><span class="line">                String cuser = userconnections.get(conn);  </span><br><span class="line">                <span class="keyword">if</span> (cuser.equals(user)) &#123;  </span><br><span class="line">                    <span class="keyword">return</span> conn;  </span><br><span class="line">                &#125;  </span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 向连接池中添加连接 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> user </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> conn </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">(String user, WebSocket conn)</span> </span>&#123;  </span><br><span class="line">        userconnections.put(conn, user); <span class="comment">// 添加连接  </span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取所有连接池</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Set&lt;WebSocket&gt; <span class="title">getAllWebSocket</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> userconnections.keySet();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 移除连接池中的连接</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> conn</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">removeUser</span><span class="params">(WebSocket conn)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (userconnections.containsKey(conn)) &#123;</span><br><span class="line">            userconnections.remove(conn); <span class="comment">// 移除连接</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 获取所有的在线用户 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Collection&lt;String&gt; <span class="title">getOnlineUser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        List&lt;String&gt; setUsers = <span class="keyword">new</span> ArrayList&lt;String&gt;();</span><br><span class="line">        Collection&lt;String&gt; setUser = userconnections.values();  </span><br><span class="line">        <span class="keyword">for</span> (String u: setUser) &#123;  </span><br><span class="line">            setUsers.add(u);  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">return</span> setUsers;  </span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 向特定的用户发送数据 </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> conn </span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> message </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sendMessageToOnlineUser</span><span class="params">(WebSocket conn, String message)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">null</span> != conn) &#123;  </span><br><span class="line">            conn.send(message);  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 向所有在线用户发送消息</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> message</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sendMessageToOnlineAllUser</span><span class="params">(String message)</span> </span>&#123;</span><br><span class="line">        Set&lt;WebSocket&gt; keySet = userconnections.keySet();  </span><br><span class="line">        <span class="keyword">synchronized</span> (keySet) &#123;</span><br><span class="line">            <span class="keyword">for</span> (WebSocket conn : keySet) &#123;  </span><br><span class="line">                String user = userconnections.get(conn);  </span><br><span class="line">                <span class="keyword">if</span> (user != <span class="keyword">null</span>) &#123; conn.send(message);  </span><br><span class="line">                &#125;  </span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="2-创建房间类"><a href="#2-创建房间类" class="headerlink" title="2.创建房间类"></a>2.创建房间类</h4><p>为的是开启房间</p><p>ChatServer.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.dist.utils.WebSocketPool;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.WebSocket;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.handshake.ClientHandshake;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.server.WebSocketServer;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.net.InetSocketAddress;</span><br><span class="line"><span class="keyword">import</span> java.net.UnknownHostException;</span><br><span class="line"><span class="keyword">import</span> java.util.Collection;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 与不同的客户端进行通讯的思路为：</span></span><br><span class="line"><span class="comment"> * Step1：在连接成功的时候 ，向服务器发送自己的用户名 ，服务器做用户标记；</span></span><br><span class="line"><span class="comment"> * Step2: 发送消息 ，格式为“XX@XXX” ，@前面表示将要发送的对象 ，“all”表示群发 ，@后面表示发送的消息。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatServer</span> <span class="keyword">extends</span> <span class="title">WebSocketServer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> String username;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ChatServer</span><span class="params">(<span class="keyword">int</span> port)</span> <span class="keyword">throws</span> UnknownHostException </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>(<span class="keyword">new</span> InetSocketAddress(port));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ChatServer</span><span class="params">(InetSocketAddress address)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>(address);</span><br><span class="line">		System.out.println(<span class="string">"地址："</span>+address);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onOpen</span><span class="params">(WebSocket conn, ClientHandshake handshake)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		System.out.println(<span class="string">"连接："</span>+conn);</span><br><span class="line"></span><br><span class="line">		sendToAll(conn.getRemoteSocketAddress().getAddress().getHostAddress()</span><br><span class="line">				+ <span class="string">" 进入房间 ！"</span>);</span><br><span class="line"></span><br><span class="line">		System.out.println(conn.getRemoteSocketAddress().getAddress()</span><br><span class="line">				.getHostAddress()</span><br><span class="line">				+ <span class="string">" 进入房间 ！"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClose</span><span class="params">(WebSocket conn, <span class="keyword">int</span> code, String reason, <span class="keyword">boolean</span> remote)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		sendToAll(conn.getRemoteSocketAddress().getAddress().getHostAddress()</span><br><span class="line">				+ <span class="string">" 离开房间 ！"</span>);</span><br><span class="line"></span><br><span class="line">		System.out.println(conn.getRemoteSocketAddress().getAddress()</span><br><span class="line">				.getHostAddress()</span><br><span class="line">				+ <span class="string">" 离开房间 ！"</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//触发关闭事件</span></span><br><span class="line">		userLeave(conn);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息发送</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onMessage</span><span class="params">(WebSocket conn, String message)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//判断是否是第一次接收的消息</span></span><br><span class="line">		<span class="keyword">boolean</span> isfirst = <span class="keyword">true</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*sendToAll("["</span></span><br><span class="line"><span class="comment">				+ conn.getRemoteSocketAddress().getAddress().getHostAddress()</span></span><br><span class="line"><span class="comment">				+ "]" + message);*/</span></span><br><span class="line"></span><br><span class="line">		System.out.println(<span class="string">"["</span></span><br><span class="line">				+ conn.getRemoteSocketAddress().getAddress().getHostAddress()</span><br><span class="line">				+ <span class="string">"]"</span> + message);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//判断是否已在连接池中</span></span><br><span class="line">		Set&lt;WebSocket&gt; webSockets=WebSocketPool.getAllWebSocket();</span><br><span class="line">		<span class="keyword">for</span> (WebSocket webSocket : webSockets)&#123;</span><br><span class="line">			<span class="keyword">if</span> (webSocket.equals(conn))&#123;</span><br><span class="line">				isfirst =<span class="keyword">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (isfirst) &#123;</span><br><span class="line">			<span class="keyword">this</span>.username = message;</span><br><span class="line">			<span class="comment">//客户端发送消息到服务器是触发事件</span></span><br><span class="line">			<span class="keyword">if</span> (message != <span class="keyword">null</span>)&#123;</span><br><span class="line">				<span class="comment">//判断用户是否已经在线</span></span><br><span class="line">				WebSocket webSocketByUser = WebSocketPool.getWebSocketByUser(message);</span><br><span class="line">				<span class="keyword">if</span> (<span class="keyword">null</span> == webSocketByUser)&#123;</span><br><span class="line">					<span class="comment">//将用户加入连接池-在线</span></span><br><span class="line">					<span class="keyword">this</span>.userJoin(username, conn);</span><br><span class="line">					System.out.println(<span class="string">"用户"</span> + username + <span class="string">"上线,在线人数："</span> + WebSocketPool.getUserCount());</span><br><span class="line">				&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">					WebSocketPool.sendMessageToOnlineUser(conn,<span class="string">"["</span>+username+<span class="string">"] 用户已在线,请您换个用户登录！"</span>);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			String[] msg = message.split(<span class="string">"@"</span>, <span class="number">2</span>);<span class="comment">//以@为分隔符把字符串分为xxx和xxxxx两部分,msg[0]表示发送至的用户名 ，all则表示发给所有人</span></span><br><span class="line">			<span class="keyword">if</span> (msg[<span class="number">0</span>].equals(<span class="string">"all"</span>)) &#123;</span><br><span class="line">				sendToAll(msg[<span class="number">1</span>]);</span><br><span class="line">			&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="comment">//指定用户发送消息</span></span><br><span class="line">				sendMessageToUser(conn,msg[<span class="number">0</span>], msg[<span class="number">1</span>]);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//异常抛出</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(WebSocket conn, Exception e)</span> </span>&#123;</span><br><span class="line">		e.printStackTrace();</span><br><span class="line">		<span class="keyword">if</span> (conn != <span class="keyword">null</span>) &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 发送给所有进入房间的人</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">sendToAll</span><span class="params">(String text)</span> </span>&#123;</span><br><span class="line">		Collection&lt;WebSocket&gt; conns = connections();</span><br><span class="line">		<span class="keyword">synchronized</span> (conns) &#123;</span><br><span class="line">			<span class="keyword">for</span> (WebSocket client : conns) &#123;</span><br><span class="line">				client.send(text);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 测试</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException,</span></span><br><span class="line"><span class="function">			IOException </span>&#123;</span><br><span class="line">		<span class="comment">/*int port = 8887;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		ChatServer server = new ChatServer(port);</span></span><br><span class="line"><span class="comment">		server.start();</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		System.out.println("房间已开启 ，等待客户端接入 ，端口号: " + server.getPort());</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		BufferedReader webSocketIn = new BufferedReader(new InputStreamReader(</span></span><br><span class="line"><span class="comment">				System.in));</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		while (true) &#123;</span></span><br><span class="line"><span class="comment">			String stringIn = webSocketIn.readLine();</span></span><br><span class="line"><span class="comment">			// 发送给所有的聊天者</span></span><br><span class="line"><span class="comment">			server.sendToAll(stringIn);</span></span><br><span class="line"><span class="comment">		&#125;*/</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 用户下线处理</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> conn</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">userLeave</span><span class="params">(org.java_websocket.WebSocket conn)</span> </span>&#123;</span><br><span class="line">		String user = WebSocketPool.getUserByKey(conn);</span><br><span class="line">		<span class="keyword">boolean</span> b = WebSocketPool.removeUser(conn); <span class="comment">// 在连接池中移除连接</span></span><br><span class="line">		<span class="keyword">if</span> (b) &#123;</span><br><span class="line">			WebSocketPool.sendMessageToOnlineAllUser(user); <span class="comment">// 把当前用户从所有在线用户列表中删除</span></span><br><span class="line">			String leaveMsg = <span class="string">"[系统]"</span> + user + <span class="string">"下线了"</span>;</span><br><span class="line">			WebSocketPool.sendMessageToOnlineAllUser(leaveMsg); <span class="comment">// 向在线用户发送当前用户退出的信息</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 用户上线处理</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> user</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> conn</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">userJoin</span><span class="params">(String user, org.java_websocket.WebSocket conn)</span> </span>&#123;</span><br><span class="line">		WebSocketPool.sendMessageToOnlineAllUser(user); <span class="comment">// 把当前用户加入到所有在线用户列表中</span></span><br><span class="line">		String joinMsg = <span class="string">"[系统]"</span> + user + <span class="string">"上线了！"</span>;</span><br><span class="line">		WebSocketPool.sendMessageToOnlineAllUser(joinMsg); <span class="comment">// 向所有在线用户推送当前用户上线的消息</span></span><br><span class="line">		WebSocketPool.addUser(user, conn); <span class="comment">// 向连接池添加当前的连接的对象</span></span><br><span class="line">		WebSocketPool.sendMessageToOnlineUser(conn, WebSocketPool.getOnlineUser().toString());  <span class="comment">// 向当前连接发送当前在线用户的列表</span></span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 发送消息给指定用户</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> currentConnection 当前连接</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> user  发送对象</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> message 消息</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendMessageToUser</span><span class="params">(WebSocket currentConnection,String user,String message)</span></span>&#123;</span><br><span class="line">		WebSocket conn = WebSocketPool.getWebSocketByUser(user);  <span class="comment">//获取发送对象用户的在线连接</span></span><br><span class="line">		<span class="keyword">if</span> (<span class="keyword">null</span> != conn)&#123;</span><br><span class="line">			<span class="comment">//向特定在线用户发送消息</span></span><br><span class="line">			WebSocketPool.sendMessageToOnlineUser(conn,message);</span><br><span class="line">			<span class="comment">//同时发送消息给当前用户</span></span><br><span class="line">            WebSocketPool.sendMessageToOnlineUser(currentConnection,message);</span><br><span class="line">		&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">			WebSocketPool.sendMessageToOnlineUser(currentConnection,<span class="string">"["</span>+user+<span class="string">"] 用户不在线,请您稍后发送！"</span>);</span><br><span class="line">			System.out.println(<span class="string">"["</span>+user+<span class="string">"] 用户不在线,请您稍后发送！"</span>);</span><br><span class="line">			WebSocketPool.sendMessageToOnlineUser(currentConnection,<span class="string">"当前在线人数："</span>+WebSocketPool.getUserCount()+<span class="string">"  有："</span>+WebSocketPool.getOnlineUser());</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="3-Http发送消息"><a href="#3-Http发送消息" class="headerlink" title="3.Http发送消息"></a>3.Http发送消息</h4><p>在controller层 创建WebSocketController.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.dist.utils.WebSocketPool;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.WebSocket;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMethod;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestParam;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/4/17 14:54</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/WebSocket"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebSocketController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  	 <span class="comment">//推送给所有在线用户</span></span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v1/sendAllUser"</span>, method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendAllUser</span><span class="params">(@RequestParam String message)</span></span>&#123;</span><br><span class="line">        <span class="comment">//推送给所有在线用户</span></span><br><span class="line">        WebSocketPool.sendMessageToOnlineAllUser(message);</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">//推送给在线的指定用户</span></span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"v1/sendUser"</span>, method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">loginWithDevice</span><span class="params">(@RequestParam String pushObject,</span></span></span><br><span class="line"><span class="function"><span class="params">                                @RequestParam String message)</span></span>&#123;</span><br><span class="line">        <span class="comment">//推送的设备对象</span></span><br><span class="line">        List&lt;String&gt; userlist = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">if</span> (pushObject == <span class="keyword">null</span> || <span class="string">""</span>.equals(pushObject)) &#123;</span><br><span class="line">            System.out.println(<span class="string">"推送对象不能为nuul"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            userlist = <span class="keyword">new</span> ArrayList&lt;&gt;(Arrays.asList(pushObject.split(<span class="string">","</span>)));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (String user : userlist)&#123;</span><br><span class="line">            <span class="comment">//根据用户查找推送的连接对象是否在线</span></span><br><span class="line">            WebSocket webSocketByUser = WebSocketPool.getWebSocketByUser(user);</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">null</span> != webSocketByUser)&#123;</span><br><span class="line">                <span class="comment">//推送给在线的指定用户</span></span><br><span class="line">                WebSocketPool.sendMessageToOnlineUser(webSocketByUser,message);</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">//未推送成功处理模块</span></span><br><span class="line">                System.out.println(<span class="string">"用户 【"</span>+user+<span class="string">"】 不在线 ，推送失败 ！ "</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="4-创建启动任务"><a href="#4-创建启动任务" class="headerlink" title="4.创建启动任务"></a>4.创建启动任务</h4><p>项目起动后自动启动这个任务-开启房间</p><p>TestStarts.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.dist.service.ChatServer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.InitializingBean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestStarts</span> <span class="keyword">implements</span> <span class="title">InitializingBean</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterPropertiesSet</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> port = <span class="number">8887</span>;</span><br><span class="line"></span><br><span class="line">        ChatServer server = <span class="keyword">new</span> ChatServer(port);</span><br><span class="line">        server.start();</span><br><span class="line">        System.out.println(<span class="string">"项目启动中：房间已开启"</span>);</span><br><span class="line">        System.out.println(<span class="string">"等待客户端接入的端口号: "</span> + server.getPort());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="H5前端代码："><a href="#H5前端代码：" class="headerlink" title="H5前端代码："></a>H5前端代码：</h3><p>H5测试页面：引入jquery.js (放同级目录)<br><img src="https://upload-images.jianshu.io/upload_images/15645795-54c7d906ce153b97.png" alt="页面目录.png"></p><p>目前测试页面上的数据信息是写死的 ，自己可以重后台获取数据 ，写成动态的</p><h5 id="测试页面1"><a href="#测试页面1" class="headerlink" title="测试页面1"></a><strong>测试页面1</strong></h5><p>H5-WebSocket.html</p><p>注意：var wsUri =”ws://129.204.207.127:8887”; 把地址改成自己的服务器地址 ，端口不是服务器的 ，是TestStarts.java启动任务自定义创建的</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span> <span class="attr">xmlns</span>=<span class="string">" http://www.w3.org/1999/html"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>H5-WebSocket Test<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"jquery.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">br</span> <span class="attr">name</span>=<span class="string">"#"</span> <span class="attr">method</span>=<span class="string">"post"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--YOUR DATA AREA--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span> <span class="attr">id</span>=<span class="string">"a"</span>&gt;</span>当前用户<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"loginsel"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"2"</span>&gt;</span>李四<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"3"</span>&gt;</span>张三<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"4"</span>&gt;</span>王二<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"连接"</span> <span class="attr">id</span>=<span class="string">"btnconnection"</span> <span class="attr">onclick</span>=<span class="string">"connectionAction()"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"断开"</span> <span class="attr">id</span>=<span class="string">"btndisconnect"</span> <span class="attr">onclick</span>=<span class="string">"disconnectAction()"</span>/&gt;</span><span class="tag">&lt;/<span class="name">br</span>&gt;</span><span class="tag">&lt;/<span class="name">br</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span> <span class="attr">id</span>=<span class="string">"b"</span>&gt;</span>发送对象<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"sel"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"1"</span> <span class="attr">selected</span>&gt;</span>all<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"2"</span>&gt;</span>李四<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"3"</span>&gt;</span>张三<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"4"</span>&gt;</span>王二<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span> <span class="attr">id</span>=<span class="string">"msg"</span>&gt;</span>要发送的信息<span class="tag">&lt;/<span class="name">label</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">id</span>=<span class="string">"message"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">id</span>=<span class="string">"btnsendMessage"</span> <span class="attr">value</span>=<span class="string">"发送消息"</span> <span class="attr">onclick</span>=<span class="string">"sendMessageAction()"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">language</span>=<span class="string">"javascript"</span><span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> wsUri =<span class="string">"ws://129.204.207.127:8887"</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> output;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> message;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> userToMessage;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> websocket;</span></span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">connectionAction</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">        output = <span class="built_in">document</span>.getElementById(<span class="string">"output"</span>);</span></span><br><span class="line"><span class="actionscript">        <span class="comment">//websocket的房间</span></span></span><br><span class="line"><span class="actionscript">        websocket = <span class="keyword">new</span> WebSocket(wsUri);</span></span><br><span class="line"><span class="actionscript">        <span class="comment">//打开连接</span></span></span><br><span class="line"><span class="actionscript">        websocket.onopen = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> loginuser = $(<span class="string">'#loginsel option:selected'</span>).text();</span></span><br><span class="line">            websocket.send(loginuser);</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onmessage = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onMessage(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onerror = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onError(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="javascript">        $(<span class="string">"#loginsel"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnconnection"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btndisconnect"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#sel"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#msg"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnsendMessage"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="comment">//给指定用户发送消息</span></span></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">sendMessageAction</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> sendTouser = $(<span class="string">'#sel option:selected'</span>).text();</span></span><br><span class="line"><span class="javascript">        message =<span class="built_in">document</span>.getElementById(<span class="string">"message"</span>).value;</span></span><br><span class="line"><span class="actionscript">        userToMessage = sendTouser +<span class="string">"@"</span>+message;</span></span><br><span class="line">        websocket.send(userToMessage);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="comment">//断开连接</span></span></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">disconnectAction</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line">        websocket.close();</span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"您已断开连接"</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">        $(<span class="string">"#loginsel"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnconnection"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btndisconnect"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#sel"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#msg"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnsendMessage"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  /*  function init() &#123;</span><br><span class="line"><span class="javascript">        output = <span class="built_in">document</span>.getElementById(<span class="string">"output"</span>);</span></span><br><span class="line">        testWebSocket();</span><br><span class="line">    &#125;</span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">testWebSocket</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        websocket = <span class="keyword">new</span> WebSocket(wsUri);</span></span><br><span class="line"><span class="actionscript">        websocket.onopen = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onOpen(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onmessage = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onMessage(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onerror = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onError(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onclose = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onClose(evt)</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;*/</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onOpen</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"CONNECTED"</span>);</span></span><br><span class="line"><span class="actionscript">        doSend(<span class="string">"Zhansna"</span>);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onClose</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"您已断开连接"</span>);</span></span><br><span class="line">        websocket.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="comment">//接收消息</span></span></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onMessage</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="handlebars"><span class="xml">        writeToScreen('<span class="tag">&lt;<span class="name">span</span> <span class="attr">style</span>=<span class="string">"color: blue;"</span>&gt;</span>RESPONSE: '+ evt.data+'<span class="tag">&lt;/<span class="name">span</span>&gt;</span>');</span></span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onError</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="handlebars"><span class="xml">        writeToScreen('<span class="tag">&lt;<span class="name">span</span> <span class="attr">style</span>=<span class="string">"color: red;"</span>&gt;</span>ERROR:<span class="tag">&lt;/<span class="name">span</span>&gt;</span> '+ evt.data);</span></span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">doSend</span><span class="params">(message)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"SENT: "</span> + message);</span></span><br><span class="line">        websocket.send(message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">writeToScreen</span><span class="params">(message)</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> pre = <span class="built_in">document</span>.createElement(<span class="string">"p"</span>);</span></span><br><span class="line"><span class="actionscript">        pre.style.wordWrap = <span class="string">"break-word"</span>;</span></span><br><span class="line">        pre.innerHTML = message;</span><br><span class="line">        output.appendChild(pre);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.addEventListener(<span class="string">"load"</span>, init, <span class="literal">false</span>);</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>WebSocket Test<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"output"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure><h5 id="测试页面2"><a href="#测试页面2" class="headerlink" title="测试页面2"></a><strong>测试页面2</strong></h5><p>H5-WebSocket2.html</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span> <span class="attr">xmlns</span>=<span class="string">" http://www.w3.org/1999/html"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>H5-WebSocket Test2<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"jquery.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">br</span> <span class="attr">name</span>=<span class="string">"#"</span> <span class="attr">method</span>=<span class="string">"post"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--YOUR DATA AREA--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span> <span class="attr">id</span>=<span class="string">"a"</span>&gt;</span>当前用户<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"loginsel"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"2"</span>&gt;</span>李四<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"3"</span>&gt;</span>张三<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"4"</span>&gt;</span>王二<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"连接"</span> <span class="attr">id</span>=<span class="string">"btnconnection"</span> <span class="attr">onclick</span>=<span class="string">"connectionAction()"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"断开"</span> <span class="attr">id</span>=<span class="string">"btndisconnect"</span> <span class="attr">onclick</span>=<span class="string">"disconnectAction()"</span>/&gt;</span><span class="tag">&lt;/<span class="name">br</span>&gt;</span><span class="tag">&lt;/<span class="name">br</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span> <span class="attr">id</span>=<span class="string">"b"</span>&gt;</span>发送对象<span class="tag">&lt;/<span class="name">label</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"sel"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"1"</span> <span class="attr">selected</span>&gt;</span>all<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"2"</span>&gt;</span>李四<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"3"</span>&gt;</span>张三<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"4"</span>&gt;</span>王二<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">label</span> <span class="attr">id</span>=<span class="string">"msg"</span>&gt;</span>要发送的信息<span class="tag">&lt;/<span class="name">label</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">id</span>=<span class="string">"message"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">id</span>=<span class="string">"btnsendMessage"</span> <span class="attr">value</span>=<span class="string">"发送消息"</span> <span class="attr">onclick</span>=<span class="string">"sendMessageAction()"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">language</span>=<span class="string">"javascript"</span><span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> wsUri =<span class="string">"ws://129.204.207.127:8887"</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> output;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> message;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> userToMessage;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> websocket;</span></span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">connectionAction</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">        output = <span class="built_in">document</span>.getElementById(<span class="string">"output"</span>);</span></span><br><span class="line"><span class="actionscript">        <span class="comment">//websocket的房间</span></span></span><br><span class="line"><span class="actionscript">        websocket = <span class="keyword">new</span> WebSocket(wsUri);</span></span><br><span class="line"><span class="actionscript">        <span class="comment">//打开连接</span></span></span><br><span class="line"><span class="actionscript">        websocket.onopen = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> loginuser = $(<span class="string">'#loginsel option:selected'</span>).text();</span></span><br><span class="line">            websocket.send(loginuser);</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onmessage = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onMessage(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onerror = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onError(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="javascript">        $(<span class="string">"#loginsel"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnconnection"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btndisconnect"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#sel"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#msg"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnsendMessage"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="comment">//给指定用户发送消息</span></span></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">sendMessageAction</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> sendTouser = $(<span class="string">'#sel option:selected'</span>).text();</span></span><br><span class="line"><span class="javascript">        message =<span class="built_in">document</span>.getElementById(<span class="string">"message"</span>).value;</span></span><br><span class="line"><span class="actionscript">        userToMessage = sendTouser +<span class="string">"@"</span>+message;</span></span><br><span class="line">        websocket.send(userToMessage);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="comment">//断开连接</span></span></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">disconnectAction</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line">        websocket.close();</span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"您已断开连接"</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">        $(<span class="string">"#loginsel"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnconnection"</span>).removeAttr(<span class="string">"disabled"</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btndisconnect"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#sel"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#msg"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">"#btnsendMessage"</span>).attr(<span class="string">"disabled"</span>,<span class="string">"disabled"</span>);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  /*  function init() &#123;</span><br><span class="line"><span class="javascript">        output = <span class="built_in">document</span>.getElementById(<span class="string">"output"</span>);</span></span><br><span class="line">        testWebSocket();</span><br><span class="line">    &#125;</span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">testWebSocket</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        websocket = <span class="keyword">new</span> WebSocket(wsUri);</span></span><br><span class="line"><span class="actionscript">        websocket.onopen = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onOpen(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onmessage = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onMessage(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onerror = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onError(evt)</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        websocket.onclose = <span class="function"><span class="keyword">function</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line">            onClose(evt)</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;*/</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onOpen</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"CONNECTED"</span>);</span></span><br><span class="line"><span class="actionscript">        doSend(<span class="string">"Zhansna"</span>);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onClose</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"您已断开连接"</span>);</span></span><br><span class="line">        websocket.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="comment">//接收消息</span></span></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onMessage</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="handlebars"><span class="xml">        writeToScreen('<span class="tag">&lt;<span class="name">span</span> <span class="attr">style</span>=<span class="string">"color: blue;"</span>&gt;</span>RESPONSE: '+ evt.data+'<span class="tag">&lt;/<span class="name">span</span>&gt;</span>');</span></span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">onError</span><span class="params">(evt)</span> </span>&#123;</span></span><br><span class="line"><span class="handlebars"><span class="xml">        writeToScreen('<span class="tag">&lt;<span class="name">span</span> <span class="attr">style</span>=<span class="string">"color: red;"</span>&gt;</span>ERROR:<span class="tag">&lt;/<span class="name">span</span>&gt;</span> '+ evt.data);</span></span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">doSend</span><span class="params">(message)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">        writeToScreen(<span class="string">"SENT: "</span> + message);</span></span><br><span class="line">        websocket.send(message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="actionscript">    <span class="function"><span class="keyword">function</span> <span class="title">writeToScreen</span><span class="params">(message)</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> pre = <span class="built_in">document</span>.createElement(<span class="string">"p"</span>);</span></span><br><span class="line"><span class="actionscript">        pre.style.wordWrap = <span class="string">"break-word"</span>;</span></span><br><span class="line">        pre.innerHTML = message;</span><br><span class="line">        output.appendChild(pre);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.addEventListener(<span class="string">"load"</span>, init, <span class="literal">false</span>);</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>WebSocket Test2<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"output"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure><p>现在就可以实现H5页面之间的通信啦<br>以上只是H5测试用例 ，真正用的项目中也是这样 ，只不过 ，要根据项目需求该代码逻辑。我的项目就是版本推送和反馈消息的及时推送；</p><h3 id="Andorid测试代码："><a href="#Andorid测试代码：" class="headerlink" title="Andorid测试代码："></a>Andorid测试代码：</h3><p>测试最好创建空白的Andorid app</p><p>在.gradle 引入java-websocket依赖</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">dependencies&#123;</span><br><span class="line">  implementation &#39;org.java-websocket:Java-WebSocket:1.3.0&#39;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h5 id="页面代码"><a href="#页面代码" class="headerlink" title="页面代码"></a>页面代码</h5><p>activity_main.xml</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:android</span>=<span class="string">" http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">" http://schemas.android.com/tools"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:orientation</span>=<span class="string">"vertical"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:paddingBottom</span>=<span class="string">"@dimen/activity_vertical_margin"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:paddingLeft</span>=<span class="string">"@dimen/activity_horizontal_margin"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:paddingRight</span>=<span class="string">"@dimen/activity_horizontal_margin"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:paddingTop</span>=<span class="string">"@dimen/activity_vertical_margin"</span></span></span><br><span class="line"><span class="tag">    &gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">ScrollView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/svChat"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_weight</span>=<span class="string">"1"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:orientation</span>=<span class="string">"vertical"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">Spinner</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/spDraft"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">&lt;!-- ws://192.168.1.104:80/JSR356-WebSocket/websocket/ --&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- ws://192.168.1.104:8887 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:orientation</span>=<span class="string">"horizontal"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;<span class="name">EditText</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:id</span>=<span class="string">"@+id/etAddress"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_width</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_weight</span>=<span class="string">"4"</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;<span class="name">Spinner</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:id</span>=<span class="string">"@+id/spAddress"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_width</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_weight</span>=<span class="string">"2"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:orientation</span>=<span class="string">"horizontal"</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--选择登录用户--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">EditText</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:id</span>=<span class="string">"@+id/etloginName"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_width</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_weight</span>=<span class="string">"4"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:hint</span>=<span class="string">"选着登录名"</span> /&gt;</span><span class="comment">&lt;!--Jack--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">Spinner</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:id</span>=<span class="string">"@+id/sploginName"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_width</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">android:layout_weight</span>=<span class="string">"2"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">Button</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/btnConnect"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:text</span>=<span class="string">"连接"</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">Button</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/btnClose"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:text</span>=<span class="string">"断开"</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">EditText</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/etDetails"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:background</span>=<span class="string">"@null"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:focusable</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:focusableInTouchMode</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:paddingTop</span>=<span class="string">"5dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:paddingBottom</span>=<span class="string">"5dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:textSize</span>=<span class="string">"13dp"</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">Button</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/btnClearDetails"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:text</span>=<span class="string">"清除聊天记录"</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">ScrollView</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:orientation</span>=<span class="string">"horizontal"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">Spinner</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/spName"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_weight</span>=<span class="string">"3"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--&lt;EditText</span></span><br><span class="line"><span class="comment">            android:id="@+id/etName"</span></span><br><span class="line"><span class="comment">            android:layout_width="wrap_content"</span></span><br><span class="line"><span class="comment">            android:layout_height="match_parent"</span></span><br><span class="line"><span class="comment">            android:hint="发送对象"</span></span><br><span class="line"><span class="comment">            android:text=""</span></span><br><span class="line"><span class="comment">            android:layout_weight="1"/&gt;--&gt;</span><span class="comment">&lt;!--Jack--&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">EditText</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/etMessage"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:hint</span>=<span class="string">"发送消息内容"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_weight</span>=<span class="string">"4"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">Button</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/btnSend"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"发送"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br></pre></td></tr></table></figure><h5 id="逻辑代码"><a href="#逻辑代码" class="headerlink" title="逻辑代码"></a>逻辑代码</h5><p>MainActivity.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.myapplication;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.support.v7.app.AppCompatActivity;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.util.Log;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.AdapterView;</span><br><span class="line"><span class="keyword">import</span> android.widget.ArrayAdapter;</span><br><span class="line"><span class="keyword">import</span> android.widget.Button;</span><br><span class="line"><span class="keyword">import</span> android.widget.EditText;</span><br><span class="line"><span class="keyword">import</span> android.widget.ScrollView;</span><br><span class="line"><span class="keyword">import</span> android.widget.Spinner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.java_websocket.WebSocketImpl;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.client.WebSocketClient;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.drafts.Draft;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.drafts.Draft_10;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.drafts.Draft_17;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.drafts.Draft_75;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.drafts.Draft_76;</span><br><span class="line"><span class="keyword">import</span> org.java_websocket.handshake.ServerHandshake;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.net.URI;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnClickListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ScrollView svChat;</span><br><span class="line">    <span class="keyword">private</span> Spinner spDraft;</span><br><span class="line">    <span class="keyword">private</span> EditText etAddress;</span><br><span class="line">    <span class="keyword">private</span> Spinner spAddress;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Spinner sploginName;</span><br><span class="line">    <span class="keyword">private</span> EditText etloginName;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Button btnConnect;</span><br><span class="line">    <span class="keyword">private</span> Button btnClose;</span><br><span class="line">    <span class="keyword">private</span> EditText etDetails;</span><br><span class="line">    <span class="keyword">private</span> Button btnClearDetails;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Spinner spName;</span><br><span class="line">    <span class="comment">//private EditText etName;</span></span><br><span class="line">    <span class="keyword">private</span> EditText etMessage;</span><br><span class="line">    <span class="keyword">private</span> Button btnSend;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> WebSocketClient client;<span class="comment">// 连接客户端</span></span><br><span class="line">    <span class="keyword">private</span> DraftInfo selectDraft;<span class="comment">// 连接协议</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line"></span><br><span class="line">        svChat = (ScrollView) findViewById(R.id.svChat);</span><br><span class="line">        spDraft = (Spinner) findViewById(R.id.spDraft);</span><br><span class="line">        etAddress = (EditText) findViewById(R.id.etAddress);</span><br><span class="line">        spAddress = (Spinner) findViewById(R.id.spAddress);</span><br><span class="line"></span><br><span class="line">        sploginName = (Spinner) findViewById(R.id.sploginName);</span><br><span class="line">        etloginName = (EditText) findViewById(R.id.etloginName);</span><br><span class="line"></span><br><span class="line">        btnConnect = (Button) findViewById(R.id.btnConnect);</span><br><span class="line">        btnClose = (Button) findViewById(R.id.btnClose);</span><br><span class="line">        etDetails = (EditText) findViewById(R.id.etDetails);</span><br><span class="line">        btnClearDetails = (Button) findViewById(R.id.btnClearDetails);</span><br><span class="line"></span><br><span class="line">        spName = (Spinner) findViewById(R.id.spName);</span><br><span class="line">        <span class="comment">//etName = (EditText) findViewById(R.id.etName);</span></span><br><span class="line">        etMessage = (EditText) findViewById(R.id.etMessage);</span><br><span class="line">        btnSend = (Button) findViewById(R.id.btnSend);</span><br><span class="line"></span><br><span class="line">        DraftInfo[] draftInfos = &#123;<span class="keyword">new</span> DraftInfo(<span class="string">"WebSocket协议Draft_17"</span>, <span class="keyword">new</span> Draft_17()), <span class="keyword">new</span> DraftInfo</span><br><span class="line">                (<span class="string">"WebSocket协议Draft_10"</span>, <span class="keyword">new</span> Draft_10()), <span class="keyword">new</span> DraftInfo(<span class="string">"WebSocket协议Draft_76"</span>, <span class="keyword">new</span> Draft_76()), <span class="keyword">new</span></span><br><span class="line">                DraftInfo(<span class="string">"WebSocket协议Draft_75"</span>, <span class="keyword">new</span> Draft_75())&#125;;<span class="comment">// 所有连接协议</span></span><br><span class="line">        selectDraft = draftInfos[<span class="number">0</span>];<span class="comment">// 默认选择第一个连接协议</span></span><br><span class="line"></span><br><span class="line">        ArrayAdapter&lt;DraftInfo&gt; draftAdapter = <span class="keyword">new</span> ArrayAdapter&lt;DraftInfo&gt;(<span class="keyword">this</span>, android.R.layout</span><br><span class="line">                .simple_spinner_item, draftInfos);</span><br><span class="line">        spDraft.setAdapter(draftAdapter);</span><br><span class="line">        spDraft.setOnItemSelectedListener(<span class="keyword">new</span> AdapterView.OnItemSelectedListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onItemSelected</span><span class="params">(AdapterView&lt;?&gt; parent, View view, <span class="keyword">int</span> position, <span class="keyword">long</span> id)</span> </span>&#123;</span><br><span class="line">                selectDraft = (DraftInfo) spDraft.getItemAtPosition(position);<span class="comment">// 选择连接协议</span></span><br><span class="line"></span><br><span class="line">                etDetails.append(<span class="string">"当前连接协议："</span> + selectDraft.draftName + <span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"选择连接协议："</span> + selectDraft.draftName);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNothingSelected</span><span class="params">(AdapterView&lt;?&gt; parent)</span> </span>&#123;</span><br><span class="line">                selectDraft = <span class="keyword">null</span>;<span class="comment">// 清空</span></span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"未选择任何连接协议"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        ServerInfo[] serverInfos = &#123;<span class="keyword">new</span> ServerInfo(<span class="string">"连接内网web后台"</span>, <span class="string">"ws://192.168.2.114:8887"</span>),<span class="keyword">new</span> ServerInfo(<span class="string">"连接外网web后台"</span>, <span class="string">"ws://129.204....:8887"</span>)&#125;;<span class="comment">// 所有连接后台//外网的自己放到服务器上</span></span><br><span class="line">        etAddress.setText(serverInfos[<span class="number">0</span>].serverAddress);<span class="comment">// 默认选择第一个连接协议</span></span><br><span class="line"></span><br><span class="line">        ArrayAdapter&lt;ServerInfo&gt; serverAdapter = <span class="keyword">new</span> ArrayAdapter&lt;ServerInfo&gt;(<span class="keyword">this</span>, android.R.layout</span><br><span class="line">                .simple_spinner_item, serverInfos);</span><br><span class="line">        spAddress.setAdapter(serverAdapter);</span><br><span class="line">        spAddress.setOnItemSelectedListener(<span class="keyword">new</span> AdapterView.OnItemSelectedListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onItemSelected</span><span class="params">(AdapterView&lt;?&gt; parent, View view, <span class="keyword">int</span> position, <span class="keyword">long</span> id)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">                ServerInfo selectServerInfo = (ServerInfo) spAddress.getItemAtPosition(position);<span class="comment">// 选择连接后台</span></span><br><span class="line">                etAddress.setText(selectServerInfo.serverAddress);</span><br><span class="line"></span><br><span class="line">                etDetails.append(<span class="string">"当前连接后台："</span> + selectServerInfo.serverName + <span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"当前连接后台："</span> + selectServerInfo.serverName);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNothingSelected</span><span class="params">(AdapterView&lt;?&gt; parent)</span> </span>&#123;</span><br><span class="line">                selectDraft = <span class="keyword">null</span>;<span class="comment">// 清空</span></span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"未选择任何连接后台"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        UserNameInfo[] loginNameInfo = &#123;<span class="keyword">new</span> UserNameInfo(<span class="string">"老邓"</span>, <span class="string">"老邓"</span>), <span class="keyword">new</span> UserNameInfo(<span class="string">"老郑"</span>, <span class="string">"老郑"</span>),<span class="keyword">new</span> UserNameInfo(<span class="string">"小金鱼"</span>, <span class="string">"小金鱼"</span>),</span><br><span class="line">                <span class="keyword">new</span> UserNameInfo(<span class="string">"张三"</span>, <span class="string">"张三"</span>),<span class="keyword">new</span> UserNameInfo(<span class="string">"李四"</span>, <span class="string">"李四"</span>),<span class="keyword">new</span> UserNameInfo(<span class="string">"王二"</span>, <span class="string">"王二"</span>)&#125;;<span class="comment">// 登录对象</span></span><br><span class="line">        etloginName.setText(loginNameInfo[<span class="number">0</span>].name);<span class="comment">// 默认选择第一个用户登录</span></span><br><span class="line"></span><br><span class="line">        ArrayAdapter&lt;UserNameInfo&gt; loginNameAdapter = <span class="keyword">new</span> ArrayAdapter&lt;UserNameInfo&gt;(<span class="keyword">this</span>, android.R.layout</span><br><span class="line">                .simple_spinner_item, loginNameInfo);</span><br><span class="line">        sploginName.setAdapter(loginNameAdapter);</span><br><span class="line">        sploginName.setOnItemSelectedListener(<span class="keyword">new</span> AdapterView.OnItemSelectedListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onItemSelected</span><span class="params">(AdapterView&lt;?&gt; parent, View view, <span class="keyword">int</span> position, <span class="keyword">long</span> id)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">                UserNameInfo selectloginNameInfo = (UserNameInfo) sploginName.getItemAtPosition(position);<span class="comment">// 选择连接后台</span></span><br><span class="line">                etloginName.setText(selectloginNameInfo.name);</span><br><span class="line"></span><br><span class="line">                etDetails.append(<span class="string">"当前登录用户名："</span> + selectloginNameInfo.loginName + <span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"当前登录用户名："</span> + selectloginNameInfo.loginName);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNothingSelected</span><span class="params">(AdapterView&lt;?&gt; parent)</span> </span>&#123;</span><br><span class="line">                etloginName = <span class="keyword">null</span>;<span class="comment">// 清空</span></span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"未选任何登录对象"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        UserNameInfo[] userNameInfo = &#123;<span class="keyword">new</span> UserNameInfo(<span class="string">"all"</span>, <span class="string">"all"</span>),<span class="keyword">new</span> UserNameInfo(<span class="string">"老邓"</span>, <span class="string">"老邓"</span>), <span class="keyword">new</span> UserNameInfo(<span class="string">"老郑"</span>, <span class="string">"老郑"</span>),<span class="keyword">new</span> UserNameInfo(<span class="string">"小金鱼"</span>, <span class="string">"小金鱼"</span>),</span><br><span class="line">                <span class="keyword">new</span> UserNameInfo(<span class="string">"李四"</span>, <span class="string">"李四"</span>), <span class="keyword">new</span> UserNameInfo(<span class="string">"张三"</span>, <span class="string">"张三"</span>), <span class="keyword">new</span> UserNameInfo(<span class="string">"王二"</span>, <span class="string">"王二"</span>)&#125;;<span class="comment">// 要发送的对象</span></span><br><span class="line"></span><br><span class="line">        ArrayAdapter&lt;UserNameInfo&gt; userNameAdapter = <span class="keyword">new</span> ArrayAdapter&lt;UserNameInfo&gt;(<span class="keyword">this</span>, android.R.layout</span><br><span class="line">                .simple_spinner_item, userNameInfo);</span><br><span class="line">        spName.setAdapter(userNameAdapter);</span><br><span class="line">        spName.setOnItemSelectedListener(<span class="keyword">new</span> AdapterView.OnItemSelectedListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onItemSelected</span><span class="params">(AdapterView&lt;?&gt; parent, View view, <span class="keyword">int</span> position, <span class="keyword">long</span> id)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">                UserNameInfo selectuserNameInfo = (UserNameInfo) spName.getItemAtPosition(position);<span class="comment">// 选择连接后台</span></span><br><span class="line">               <span class="comment">// etName.setText(selectuserNameInfo.name);</span></span><br><span class="line"></span><br><span class="line">                etDetails.append(<span class="string">"发送对象："</span> + selectuserNameInfo.loginName + <span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"发送对象："</span> + selectuserNameInfo.loginName);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNothingSelected</span><span class="params">(AdapterView&lt;?&gt; parent)</span> </span>&#123;</span><br><span class="line">               <span class="comment">// etName = null;// 清空</span></span><br><span class="line"></span><br><span class="line">                Log.e(<span class="string">"wlf"</span>, <span class="string">"未选任何发送对象"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        btnConnect.setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line">        btnClearDetails.setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line">        btnClose.setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line">        btnSend.setOnClickListener(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        WebSocketImpl.DEBUG = <span class="keyword">true</span>;</span><br><span class="line">        System.setProperty(<span class="string">"java.net.preferIPv6Addresses"</span>, <span class="string">"false"</span>);</span><br><span class="line">        System.setProperty(<span class="string">"java.net.preferIPv4Stack"</span>, <span class="string">"true"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> (v.getId()) &#123;</span><br><span class="line">            <span class="keyword">case</span> R.id.btnConnect:</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (selectDraft == <span class="keyword">null</span>) &#123;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span>(etloginName == <span class="keyword">null</span>)&#123;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    String address = etAddress.getText().toString().trim();</span><br><span class="line">                    <span class="keyword">if</span> (address.contains(<span class="string">"JSR356-WebSocket"</span>)) &#123;</span><br><span class="line">                        <span class="comment">//address += etName.getText().toString().trim();</span></span><br><span class="line">                    &#125;</span><br><span class="line">                    Log.e(<span class="string">"wlf"</span>, <span class="string">"连接地址："</span> + address);</span><br><span class="line">                    client = <span class="keyword">new</span> WebSocketClient(<span class="keyword">new</span> URI(address), selectDraft.draft) &#123;</span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onOpen</span><span class="params">(<span class="keyword">final</span> ServerHandshake serverHandshakeData)</span> </span>&#123;</span><br><span class="line">                            runOnUiThread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                                <span class="meta">@Override</span></span><br><span class="line">                                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                                    etDetails.append(<span class="string">"已经连接到服务器【"</span> + getURI() + <span class="string">"】\n"</span>);</span><br><span class="line"></span><br><span class="line">                                    Log.e(<span class="string">"wlf"</span>, <span class="string">"已经连接到服务器【"</span> + getURI() + <span class="string">"】"</span>);</span><br><span class="line"></span><br><span class="line">                                    spDraft.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    etAddress.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    spAddress.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    etloginName.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    sploginName.setEnabled(<span class="keyword">false</span>);</span><br><span class="line"></span><br><span class="line">                                    btnConnect.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    <span class="comment">//etName.setEnabled(false);</span></span><br><span class="line"></span><br><span class="line">                                    spName.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    btnClearDetails.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    btnClose.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    btnSend.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;);</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onMessage</span><span class="params">(<span class="keyword">final</span> String message)</span> </span>&#123;</span><br><span class="line">                            runOnUiThread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                                <span class="meta">@Override</span></span><br><span class="line">                                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                                    etDetails.append(<span class="string">"获取到服务器信息【"</span> + message + <span class="string">"】\n"</span>);</span><br><span class="line"></span><br><span class="line">                                    Log.e(<span class="string">"wlf"</span>, <span class="string">"获取到服务器信息【"</span> + message + <span class="string">"】"</span>);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;);</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClose</span><span class="params">(<span class="keyword">final</span> <span class="keyword">int</span> code, <span class="keyword">final</span> String reason, <span class="keyword">final</span> <span class="keyword">boolean</span> remote)</span> </span>&#123;</span><br><span class="line">                            runOnUiThread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                                <span class="meta">@Override</span></span><br><span class="line">                                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                                    etDetails.append(<span class="string">"断开服务器连接【"</span> + getURI() + <span class="string">" ，状态码： "</span> + code + <span class="string">" ，断开原因："</span> + reason +</span><br><span class="line">                                            <span class="string">"】\n"</span>);</span><br><span class="line"></span><br><span class="line">                                    Log.e(<span class="string">"wlf"</span>, <span class="string">"断开服务器连接【"</span> + getURI() + <span class="string">" ，状态码： "</span> + code + <span class="string">" ，断开原因："</span> + reason + <span class="string">"】"</span>);</span><br><span class="line"></span><br><span class="line">                                    spDraft.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    etAddress.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    spAddress.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    etloginName.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    sploginName.setEnabled(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">                                    btnConnect.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    <span class="comment">//etName.setEnabled(true);</span></span><br><span class="line"></span><br><span class="line">                                    spName.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    btnClearDetails.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    btnClose.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    btnSend.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;);</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(<span class="keyword">final</span> Exception e)</span> </span>&#123;</span><br><span class="line">                            runOnUiThread(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                                <span class="meta">@Override</span></span><br><span class="line">                                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                                    etDetails.append(<span class="string">"连接发生了异常【异常原因："</span> + e + <span class="string">"】\n"</span>);</span><br><span class="line"></span><br><span class="line">                                    Log.e(<span class="string">"wlf"</span>, <span class="string">"连接发生了异常【异常原因："</span> + e + <span class="string">"】"</span>);</span><br><span class="line"></span><br><span class="line">                                    spDraft.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    etAddress.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    btnConnect.setEnabled(<span class="keyword">true</span>);</span><br><span class="line">                                    <span class="comment">//etName.setEnabled(true);</span></span><br><span class="line"></span><br><span class="line">                                    btnClearDetails.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    btnClose.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                    btnSend.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;;</span><br><span class="line">                    client.connect();</span><br><span class="line">                    <span class="keyword">new</span> Thread()&#123;</span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;</span><br><span class="line">                            <span class="keyword">super</span>.run();</span><br><span class="line">                            <span class="keyword">try</span> &#123;</span><br><span class="line">                                Thread.sleep(<span class="number">200</span>);</span><br><span class="line">                                <span class="keyword">if</span> (client != <span class="keyword">null</span>) &#123;</span><br><span class="line">                                    client.send(etloginName.getText().toString().trim());</span><br><span class="line">                                    svChat.post(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                                        <span class="meta">@Override</span></span><br><span class="line">                                        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                                            svChat.fullScroll(View.FOCUS_DOWN);</span><br><span class="line">                                            etMessage.setText(<span class="string">""</span>);</span><br><span class="line">                                            etMessage.requestFocus();</span><br><span class="line">                                        &#125;</span><br><span class="line">                                    &#125;);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                                e.printStackTrace();</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;.start();</span><br><span class="line"></span><br><span class="line">                &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> R.id.btnClearDetails:</span><br><span class="line">                <span class="keyword">if</span> (etDetails.getText().toString() != <span class="keyword">null</span> &amp;&amp; etDetails.getText().length()&gt;<span class="number">0</span>) &#123;</span><br><span class="line">                    etDetails.setText(<span class="string">""</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> R.id.btnClose:</span><br><span class="line">                <span class="keyword">if</span> (client != <span class="keyword">null</span>) &#123;</span><br><span class="line">                    client.close();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> R.id.btnSend:</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (client != <span class="keyword">null</span>) &#123;</span><br><span class="line">                        <span class="comment">//client.send(etName.getText().toString().trim() + "说：" + etMessage.getText().toString().trim());</span></span><br><span class="line">                        <span class="comment">//client.send(etName.getText().toString().trim() +"@"+etMessage.getText().toString().trim());</span></span><br><span class="line">                        client.send(spName.getSelectedItem().toString().trim() +<span class="string">"@"</span>+etMessage.getText().toString().trim());</span><br><span class="line">                        svChat.post(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">                            <span class="meta">@Override</span></span><br><span class="line">                            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                                svChat.fullScroll(View.FOCUS_DOWN);</span><br><span class="line">                                etMessage.setText(<span class="string">""</span>);</span><br><span class="line">                                etMessage.requestFocus();</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onDestroy</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onDestroy();</span><br><span class="line">        <span class="keyword">if</span> (client != <span class="keyword">null</span>) &#123;</span><br><span class="line">            client.close();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">DraftInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> String draftName;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> Draft draft;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">DraftInfo</span><span class="params">(String draftName, Draft draft)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.draftName = draftName;</span><br><span class="line">            <span class="keyword">this</span>.draft = draft;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> draftName;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">ServerInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> String serverName;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> String serverAddress;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">ServerInfo</span><span class="params">(String serverName, String serverAddress)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.serverName = serverName;</span><br><span class="line">            <span class="keyword">this</span>.serverAddress = serverAddress;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> serverName;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">UserNameInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> String loginName;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">final</span> String name;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">UserNameInfo</span><span class="params">(String loginName, String name)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.loginName = loginName;</span><br><span class="line">            <span class="keyword">this</span>.name = name;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> loginName;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>现在就可以完成H5和Andorid通信啦</p></div><div><div style="text-align:center;color:#ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div></div><div class="reward-container"><div><p style="font-size:14px;color:#34495e;margin:0 0 5px 0">赞赏一下吧～ 还可以关注公众号订阅最新内容</p></div><button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">打赏</button><div id="qr" style="display:none"><div style="display:inline-block"><img src="/my-images/WeChat.png" alt="宇宙小神特别萌 微信支付"><p>微信支付</p></div><div style="display:inline-block"><img src="/my-images/Alipay.png" alt="宇宙小神特别萌 支付宝"><p>支付宝</p></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/tags/WebScoket/" rel="tag"><i class="fa fa-tag"></i> WebScoket</a></div><div class="post-nav"><div class="post-nav-item"><a href="/posts/2680579140.html" rel="prev" title="Spring5x-base"><i class="fa fa-chevron-left"></i> Spring5x-base</a></div><div class="post-nav-item"><a href="/posts/3431840903.html" rel="next" title="springboot-redis-session配置两种方式">springboot-redis-session配置两种方式 <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div><div class="comments" id="valine-comments"></div><script>window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#H5和Andorid测试页面："><span class="nav-text">H5和Andorid测试页面：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#通过Http发送测试页面"><span class="nav-text">通过Http发送测试页面</span></a></li></ol></li></ol><li class="nav-item nav-level-3"><a class="nav-link" href="#java后台代码"><span class="nav-text">java后台代码</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#引入依赖："><span class="nav-text">引入依赖：</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-WebSocketPool自定义工具类"><span class="nav-text">1.WebSocketPool自定义工具类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-创建房间类"><span class="nav-text">2.创建房间类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-Http发送消息"><span class="nav-text">3.Http发送消息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-创建启动任务"><span class="nav-text">4.创建启动任务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#H5前端代码："><span class="nav-text">H5前端代码：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#测试页面1"><span class="nav-text">测试页面1</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#测试页面2"><span class="nav-text">测试页面2</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Andorid测试代码："><span class="nav-text">Andorid测试代码：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#页面代码"><span class="nav-text">页面代码</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#逻辑代码"><span class="nav-text">逻辑代码</span></a></li></ol></li></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="宇宙小神特别萌" src="/my-images/avatar.gif"><p class="site-author-name" itemprop="name">宇宙小神特别萌</p><div class="site-description" itemprop="description">人生就是一次幸运的旅行！</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">116</span> <span class="site-state-item-name">文章</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">27</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/zhengjiaao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengjiaao" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://www.jianshu.com/u/70d69269bd09" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;70d69269bd09" rel="noopener" target="_blank"><i class="fa fa-fw fa-book"></i>简书</a> </span><span class="links-of-author-item"><a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a></span></div><div class="links-of-blogroll motion-element"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> 推荐阅读</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://github.com/spring-projects" title="https:&#x2F;&#x2F;github.com&#x2F;spring-projects" rel="noopener" target="_blank">spring-projects</a></li><li class="links-of-blogroll-item"><a href="http://www.alloyteam.com/nav/" title="http:&#x2F;&#x2F;www.alloyteam.com&#x2F;nav&#x2F;" rel="noopener" target="_blank">Web前端导航</a></li><li class="links-of-blogroll-item"><a href="http://www.chuangzaoshi.com/code" title="http:&#x2F;&#x2F;www.chuangzaoshi.com&#x2F;code" rel="noopener" target="_blank">创造狮导航</a></li></ul></div></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">宇宙小神特别萌</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span title="站点总字数">1.2m</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="站点阅读时长">17:41</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-divider">|</span> <span class="post-meta-item" id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer></div><script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script><script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/js/local-search.js"></script><script>NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdnjs.cloudflare.com/ajax/libs/valine/1.3.10/Valine.min.js', () => {
    <!--var GUEST = ['nick', 'mail', 'link'];-->
    var GUEST = ['nick', 'mail'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: false,
      appId: 'OuyHeA0XGL8hK4vgbqL0iERx-gzGzoHsz',
      appKey: 'L58VglKmWULcGK2W2AN7kWzM',
      placeholder: "留下邮箱，有回复时你将收到提醒，邮箱不会被公开。",
      avatar: 'wavatar',
      meta: guest,
      pageSize: '10' || 10,
      visitor: true,
      lang: 'zh-cn' || 'zh-cn',
      path: location.pathname,
      recordIP: true,
      serverURLs: ''
    });
  }, window.Valine);
});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,model:{jsonPath:"/live2dw/assets/wanko.model.json"},display:{position:"right",width:150,height:300},mobile:{show:!1},log:!1})</script></body><script type="text/javascript" src="/js/src/activetitle.js"></script></html><!-- rebuild by neat -->